#!/usr/bin/env python2.7

import os, sys, re
import numpy as np
from metlib.shell import *
from metlib.datetime import *
from metlib.misc.misc import *
from metlib.misc.alias import *

__all__ = ['read_kp_sounding']

def read_kp_sounding(fname):
    """reads hko king's park sounding individual files.
    Parameters:
        fname: file name.
    Returns:
        A tuple of (datetime_of_sounding_in_UTC, rec), 
        or None if the file is invalid.
    """
    f = open(fname)
    dateline = f.readline().rstrip()
    if dateline == 'DATA NOT AVAILABLE':
        return None
    thedt = T(dateline)
    #print thedt
    sounding_dtype = [('pres', 'i8'), ('height', 'f8'), ('temp', 'f8'), ('rh', 'f8'), ('dew', 'f8'), ('wd', 'f8'), ('ws', 'f8')]
    # skip two more lines of header
    for i in range(2):
        f.readline()
    rec = np.genfromtxt(f, dtype=sounding_dtype)
    return thedt, rec
